package wms.handler;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import wms.entity.Operator;
import wms.entity.WareHouse;
import wms.service.repo.RepoService;
import wms.util.constant.SysConstant;

@Controller
public class RepoController extends BaseController{
	
	@Resource
	private RepoService repoService;
	
	
	@RequestMapping("/base/repolist.html")
	public String getRepoPage(Model model, HttpSession session) throws Exception{
		log.debug("仓库管理页面---------》");
		if (session.getAttribute(SysConstant.OPERATOR) == null) {
			return "redirect:/";
		}
		List<WareHouse> listAllWareHouse = repoService.listAllWareHouse();
		if (listAllWareHouse != null) {
			model.addAttribute(SysConstant.REPO_LIST, listAllWareHouse);
		} 
		return "repo/repolist";
	}

	
	
	
	@RequestMapping("/basic/repo/modify")
	@ResponseBody
	public Map<String, Object> getRepoModifyData(@RequestParam("repoCode")String repoCode) throws Exception {
		log.debug("仓库修改页面------>");
		
		Map<String, Object> map = new HashMap<String, Object>();
		
		if (repoCode == null || repoCode.equals("")) {
			map.put(SysConstant.MSG, SysConstant.FAIL);
		} else {
			WareHouse wareHouse = repoService.getWareHouse(repoCode);
			if (wareHouse != null)
				map.put(SysConstant.MSG, wareHouse);
			else 
				map.put(SysConstant.MSG, SysConstant.FAIL);
		}
		return map;
		
	}
	
	@RequestMapping(value = "/basic/repo/savemodify", method = RequestMethod.POST)
	public String modifyRepo(WareHouse wareHouse, HttpSession session, HttpServletRequest request) throws Exception {
		log.debug("仓库信息修改保存--------->");
		Operator operator = (Operator) session.getAttribute(SysConstant.OPERATOR);
		if (operator == null)
			return "redirect:/";
		
		if (repoService.txModifyRepo(wareHouse, operator.getLoginCode())) {
			return "redirect:/base/repolist.html";
		} else{
			request.setAttribute(SysConstant.MSG, SysConstant.FAIL);
			return "redirect:/";
		}
		
	}
	
	

	@RequestMapping(value = "/basic/repo/del")
	@ResponseBody
	public Map<String, String> deleteRepo(@RequestParam(value = "repoCode") String repoCode) throws Exception {
		log.debug("移除仓库----------》" + repoCode);
		Map<String, String> map = new HashMap<String, String>();
		if (repoCode == null || repoCode.equals(""))
			map.put(SysConstant.MSG, SysConstant.FAIL);
		else {
			if (repoService.txDeleteRepo(repoCode)) {
				map.put(SysConstant.MSG, SysConstant.SUCCESS);
			} else {
				map.put(SysConstant.MSG, SysConstant.FAIL);
			}
		}
		return map;
	}
	
	
	
	/**
	 * 清理仓库
	 * @param repoCode
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/basic/repo/clear")
	@ResponseBody
	public Map<String, String> clearRepo(@RequestParam(value = "repoCode") String repoCode) throws Exception {
		log.debug("清仓----------》" + repoCode);
		Map<String, String> map = new HashMap<String, String>();
		if (repoCode == null || repoCode.equals(""))
			map.put(SysConstant.MSG, SysConstant.FAIL);
		else {
			if (repoService.txClearRepo(repoCode)) {
				map.put(SysConstant.MSG, SysConstant.SUCCESS);
			} else {
				map.put(SysConstant.MSG, SysConstant.FAIL);
			}
		}
		return map;
	}
	
	@RequestMapping(value = "/basic/repo/saveadd", method = RequestMethod.POST)
	public String saveRepo(WareHouse wareHouse, HttpSession session) throws Exception {
		log.debug("新增仓库:------>"  + wareHouse.getRepoCode());
		Operator operator = (Operator) session.getAttribute(SysConstant.OPERATOR);
		if (operator == null)
			return "redirect:/";
		
		wareHouse.setCreateBy(operator.getLoginCode());
		wareHouse.setCreateDate(new Date(System.currentTimeMillis()));
		
		if(repoService.txSaveRepo(wareHouse)){
			return "redirect:/base/repolist.html";
		}
		return "redirect:/";
		
	}
	
	
	
	
}
